package com.report.reportserver.controller;

import com.report.reportserver.model.Teacher;
import com.report.reportserver.service.TeacherService;
import com.report.reportserver.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ ClassName: TeacherController
 * @ Description：查询教师信息
 * @ Modified By：
 * @ Version:
 * @ Author     ：LiYing
 * @ Date       ：Created in 16:39 2022/3/29
 */
@Controller
@RequestMapping(value = "/teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;

    @RequestMapping(value = "/id")
    public @ResponseBody
    Teacher id(HttpServletRequest request) {
        Integer id = Integer.parseInt(request.getParameter("id"));
        return teacherService.queryTeacherById(id);
    }

    @RequestMapping(value = "/name")
    public @ResponseBody
    List<Teacher> name(HttpServletRequest request) {
        String name = request.getParameter("name");
        return teacherService.queryTeacherByName(name);
    }

    @RequestMapping(value = "/number")
    public @ResponseBody
    Teacher number(HttpServletRequest request) {
        String number = request.getParameter("number");
        return teacherService.queryTeacherByNumber(number);
    }

    @RequestMapping(value = "/academy")
    public @ResponseBody
    List<Teacher> academy(HttpServletRequest request) {
        String academy = request.getParameter("academy");
        return teacherService.queryTeacherByAcademy(academy);
    }

    @RequestMapping(value = "/title")
    public @ResponseBody
    List<Teacher> title(HttpServletRequest request) {
        String title = request.getParameter("title");
        return teacherService.queryTeacherByTitle(title);
    }

    /*
     * 通过excel批量导入Teacher对象
     * @param file
     * @return java.lang.String
     * @author LiYing
     * @creed: Talk is cheap,show me the code
     * @date 2022/4/14 19:17
     */
    @RequestMapping(value = "/excelinsert")
    public @ResponseBody
    String ExcelParse(@RequestParam("file") MultipartFile file) {
        String name = file.getOriginalFilename();

        if (!name.substring(name.length() - 4).equals(".xls") && !name.substring(name.length() - 5).equals(".xlsx")) {
            return "文件解析错误";
        }
        List<Teacher> list = new ArrayList<>();
        try {
            list = ExcelUtil.excelToTeacher(file.getInputStream());

            for (Teacher teacher : list) {
                teacherService.insertTeacher(teacher);
            }
        } catch (Exception e) {
            //异常类型
            String eName = e.toString().split(":")[0];

            e.printStackTrace();

            if (eName.split("\\.").length >= 4 && eName.split("\\.")[3].equals("DuplicateKeyException")) {
                return "There is DuplicateKey in Your File";
            }
            return "error";
        }
        return "done";
    }
}
